#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        for (const auto& e : nums) sum += e;
        if (x > sum) return -1;
        int k = sum - x;
        int l = 0, r = 0;
        int n = nums.size();
        int cur = 0;
        int tgt = -1;
        while (r < n)
        {
            cur += nums[r];
            while (cur > k)
            {
                cur -= nums[l];
                l++;
            }
            r++;
            if (cur == k) tgt = max(tgt, r - l);
        }
        return tgt == -1 ? -1 : n - tgt;
    }
};